using System;
using SqlSugar;
using Infrastructure.Attribute;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.Models;
using ZR.Repository;
using ZR.Service.Auth.IAuthService;
using System.Linq;
using ZR.Common;
using ZR.Model.Dto.Auth;
using Aliyun.OSS;

namespace ZR.Service.Auth
{
    /// <summary>
    /// 认证记录表Service业务层处理
    ///
    /// @author admin
    /// @date 2023-03-06
    /// </summary>
    [AppService(ServiceType = typeof(IVeriRecordService), ServiceLifetime = LifeTime.Transient)]
    public class VeriRecordService : BaseService<VeriRecord>, IVeriRecordService
    {
        #region 业务逻辑代码
        /// <summary>
        /// 获取认证总条数
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Statistics GetCount(Seach seach)
        {
                Statistics statistics=new Statistics();
                var days = Expressionable.Create<VeriRecord>();
                var moon = Expressionable.Create<VeriRecord>();
                var all = Expressionable.Create<VeriRecord>();
                DateTime StartDate = Convert.ToDateTime(DateTime.Now.Date.ToString());
                DateTime EndDate = Convert.ToDateTime(DateTime.Now.Date.ToString()).AddDays(1).AddSeconds(-1);
                days = days.And(x => StartDate <= x.CreateTime && x.CreateTime <= EndDate);
                var daydata = Queryable() .Where(days.ToExpression()).ToPage<VeriRecord, VeriRecordDto>(seach);
                //获取当前时间
                DateTime dt = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
                //获取当前月初
                DateTime startime = dt.AddMonths(0);
                //获取当前月末
                 DateTime endtime = startime.AddMonths(1).AddSeconds(-1);
                moon = moon.And(time => startime <= time.CreateTime && time.CreateTime <= endtime);
                var moondata = Queryable().Where(moon.ToExpression()) .ToPage<VeriRecord, VeriRecordDto>(seach);    
                var alldata = Queryable() .Where(all.ToExpression()).ToPage<VeriRecord, VeriRecordDto>(seach);
                statistics.TodayCount = daydata.TotalNum;
                statistics.MonthCount = moondata.TotalNum;
                statistics.AllCount = alldata.TotalNum;
            return statistics;
        }

        /// <summary>
        /// 查询认证记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<VeriRecordDto> GetList(VeriRecordQueryDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<VeriRecord>();
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                //.OrderBy("Id desc")
                .Where(predicate.ToExpression())
                .ToPage<VeriRecord, VeriRecordDto>(parm);
           
            return response;
        }


        /// <summary>
        /// 添加认证记录表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public int AddVeriRecord(VeriRecord parm)
        {
            //生成雪花ID
            parm.Id = SnowFlakeSingle.instance.getID();

            var response = Insert(parm, it => new
            {
                it.Id,
                it.CreateTime,
                it.Phone,
                it.Name,
                it.IdNumber,
                it.Remark,
                it.State,
                it.VeriType,
                it.Avatar,
                it.OpenId,
                it.FromDevice,
            });
            return response;
        }

        /// <summary>
        /// 修改认证记录表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public int UpdateVeriRecord(VeriRecord parm)
        {
            var response = Update(w => Convert.ToString(w.Id) == parm._id, it => new VeriRecord()
            {
                CreateTime = parm.CreateTime,
                Phone = parm.Phone,
                Name = parm.Name,
                IdNumber = parm.IdNumber,
                Remark = parm.Remark,
                State = parm.State,
                VeriType = parm.VeriType,
                Avatar = parm.Avatar,
                OpenId = parm.OpenId,
                FromDevice = parm.FromDevice,
                Sign = parm.Sign,
                _id = parm._id
            }) ;
            return response;
        }

        /// <summary>
        /// 上传头像
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public int UpdateAvatar(VeriRecord parm)
        {
            var response = Update(w => w.Phone == parm.Phone, it => new VeriRecord()
            {
                Avatar = parm.Avatar
            });
            return response;
        }

        /// <summary>
        /// 清空认证记录表
        /// </summary>
        /// <returns></returns>
        public void TruncateVeriRecord()
        {
            Truncate();
        }

        /// <summary>
        /// 获取百度2要素认证token
        /// </summary>
        /// <returns></returns>
        public string GetAccessToken()
        {
            //获取百度实名认证Token
            var response = IdentityAuthHelper.getAccessToken();
            return response;
        }

        public string AuthenticationByAliyun(VeriRecord parm)
        {
            var response = IdentityAuthHelper.IdentificationByBaidu(parm.Phone,parm.IdNumber,parm.Name);
            return response;
        }

       
        #endregion
    }
}